package com.xizi.lotterySystem.dao.mapper;

import com.xizi.lotterySystem.dao.dataobject.WinningRecordDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WinningRecordMapper {


    @Insert("<script>" +
            " insert into winning_record" +
            " (activity_id,activity_name," +
            " prize_id,prize_name,prize_tier," +
            " winner_id,winner_name,winner_email,winner_phone_number,winning_time)" +
            " values" +
            " <foreach collection='items' item='item' index='index' separator=','> "+
            " (#{item.activityId},#{item.activityName}," +
            " #{item.prizeId},#{item.prizeName},#{item.prizeTier}," +
            " #{item.winnerId},#{item.winnerName},#{item.winnerEmail},#{item.winnerPhoneNumber},#{item.winningTime})"+
            " </foreach>"+
            " </script>")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int bacthInsert(@Param("items") List<WinningRecordDO> winningRecordDOList);

    @Select("select * from winning_record where activity_id = #{activityId}")
    List<WinningRecordDO> selectByActivityId(Long activityId);

    @Select("select count(*) from winning_record where activity_id = #{activityId} and prize_id = #{prizeId}")
    int selectByAPId(Long activityId, Long prizeId);

    @Delete("<script>"
            + " DELETE FROM winning_record"
            + " WHERE activity_id = #{activityId}"
            + " <if test='prizeId != null'>"
            + "   AND prize_id = #{prizeId}"
            + " </if>"
            + "</script>")
    void deleteRecords(Long activityId, Long prizeId);

    @Select("<script>"
            + " select * FROM winning_record"
            + " WHERE activity_id = #{activityId}"
            + " <if test='prizeId != null'>"
            + "   AND prize_id = #{prizeId}"
            + " </if>"
            + "</script>")
    List<WinningRecordDO> selectByActivityIdOrPrizeId(Long activityId, Long prizeId);
}
